﻿using System;
using System.Collections.Generic;
using System.Linq;
using CSP.Common.Web;
using CSP.Bll;
using CSP.Model.CSPEnum;
using CSP.Model.CSP;
using CSP.Bll.IObjectBlls;
using CSP.Common.Providers;
using System.Text;

namespace CSP.Web.MyHome.ServiceManage
{
    public partial class Add : CSPMyHome
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pdata_ad.Text = "1" + CSP.Common.Tools.CommonTool.GenerateNumberCode(3);
                BindServiceAgeGroup();

                //生成提供资料
                BindServiceCaiLiao();
                BindRepayment();
                BindServiceAreaGroup();


                //
                BindServiceTypeUI();
                BindServiceAge();
                BindServiceHouse();
                BindServiceCar();
                BindServiceTrust();
            }

        }

        private int _serviceCount = 0;

        public int ServiceCount
        {
            get { return _serviceCount; }
            set { _serviceCount = value; }
        }
        private int _ChildServiceCount = 0;

        public int ChildServiceCount
        {
            get { return _ChildServiceCount; }
            set { _ChildServiceCount = value; }
        }

        protected StringBuilder GetServiceChildUrl(string childId)
        {
            ServiceCount++;
            
            string[] childArray = childId.Split(',');
            int childLen = childArray.Length;
            int _childId = 0;
            StringBuilder sb = new StringBuilder();
            StringBuilder childsb = new StringBuilder();
            sb.Append("<select id='sel"+ServiceCount+"'>");
            sb.Append("<option value='0'>不限</option>");
            for (int i = 0; i < childLen; i++)
            {
                _childId = Int32.Parse(childArray[i]);
                //int _childId = Int32.Parse(childId);
                List<ServiceAsset> serviceAsset = GetAssetData();
                List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == _childId).ToList();
                string selectGroup = string.Empty;
                foreach (ServiceAsset item in serviceData)
                {
                    if (item.Level == 999)
                    {
                        #region
                        //下拉显示,level为999处理　
                        //selectGroup = item.Level.ToString();
                        sb = new StringBuilder();
                        sb.Append("<select id='sel" + ServiceCount + "' onchange=\"selChange(this,this.options[this.options.selectedIndex].value)\">");
                        sb.Append("<option value='0'>不限</option>");
                        //sb.Append("<option value='>" + item.Name + "</option>");
                        //获取下级
                        
                        List<ServiceAsset> serviceSubProp = GetAssetData();
                        List<ServiceAsset> __serviceSubProp = serviceSubProp.Where(s => s.Parentid == item.Id).ToList();
                        foreach (ServiceAsset _subprop in __serviceSubProp)
                        {
                            sb.Append("<option value='" + _subprop.Id + "'>" + _subprop.Name + "</option>");

                            List<ServiceAsset> serviceThirdProp = GetAssetData();
                            List<ServiceAsset> _serviceThirdProp = serviceSubProp.Where(s => s.Parentid == _subprop.Id).ToList();
                            if (_serviceThirdProp.Count > 0)
                            {
                                ChildServiceCount++;
                                childsb.Append("<div id='subsel_" + _subprop.Id + "' style=\"display:none;width: 100%; float: left; overflow: hidden;\">");
                                childsb.Append("<span class=\"spanstyle\">" + _subprop.Name + "</span>");
                                childsb.Append("<select id='subChildSel_" + ChildServiceCount + "' name='subsel_" + _subprop.Parentid + "' onchange=\"selChildChange(this.options[this.options.selectedIndex].value)\">");
                                //childsb.Append(" style=\"display:'none';\">");
                                childsb.Append("<option value='0'>不限</option>");
                                foreach (ServiceAsset __ThirdProp in _serviceThirdProp)
                                {
                                    string currrentChildValue = __ThirdProp.Parentid + "-" + __ThirdProp.Id.ToString() + "-" + __ThirdProp.val.ToString();

                                    childsb.Append("<option value='" + currrentChildValue + "'");

                                    childsb.Append(">" + __ThirdProp.Name + "</option>");
                                }

                                childsb.Append("</select>");
                                childsb.Append("</div>");
                            }
                            //sb.Append("<div class=\"ui-form-text\"><div class=\"text\"><input name='" + __name + "' type='text' value=\"3000\" /><span class=\"tail\">元</span></div></div>");
                            //startIndex++;
                        }
                        sb.Append("</select>");
                        if (childsb.Length > 0)
                        {
                            sb.Append(childsb.ToString());
                        }
                        #endregion
                    }
                    else if (item.Name == "OTHER")
                    {
                        string __name = "limit" + item.Id.ToString();
                        sb.Append("<div class=\"ui-form-text\"><div class=\"text\"><input name='" + __name + "' type='text' value=\"\" /><span class=\"tail\">元</span></div></div>");
                    }
                    else
                    {
                        string currrentValue = item.Parentid + "-" + item.Id.ToString() + "-" + item.val.ToString();
                        sb.Append("<option value='" + currrentValue + "'>" + item.Name + "</option>");
                        //sb.Append("<input id='child" + item.Id + "' type=\"radio\" name='radio" + item.Parentid + "' value='" + item.Id + "' /><label for='child" + item.Id + "'>" + item.Name + "</label>");
                    }
                }
            }
            sb.Append("</select>");
            return sb;
        }

        private string _subselString = string.Empty;

        public string SubselString
        {
            get { return _subselString; }
            set { _subselString = value; }
        }
        private void BindServiceTypeUI()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            //ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 9);

            //List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == house.Parentid).ToList();
            //this.R_Type.DataSource = serviceData;
            //this.R_Type.DataBind();


            if (string.IsNullOrWhiteSpace(RequestServiceType))
            {
                List<ServiceAsset> servicetypes = serviceAsset.Where(s => s.Parentid == 52).ToList();
                this.R_ServiceType.DataSource = servicetypes;
                this.R_ServiceType.DataBind();
                List<ServiceAsset> servicetypesEnterPrise = serviceAsset.Where(s => s.Parentid == 53).ToList();
                this.R_EnterPriseType.DataSource = servicetypesEnterPrise;
                this.R_EnterPriseType.DataBind();

            }

        }

        private string _requestServiceType = string.Empty;

        private string _requestServiceTypeSub = string.Empty;
        protected string RequestServiceType
        {
            get
            {
                return _requestServiceType;
            }
            set
            {
                _requestServiceType = value;
            }
        }

        protected string RequestServiceTypeSub
        {
            get
            {
                return _requestServiceTypeSub;
            }
        }
        public void BindServiceAge()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset age = serviceAsset.FirstOrDefault(s => s.Parentid == 7);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == age.Parentid).ToList();
            this.ddl_Age.DataTextField = "name";
            this.ddl_Age.DataValueField = "id";
            this.ddl_Age.DataSource = serviceData;
            this.ddl_Age.DataBind();
            //this.ddl_Age.Items.Add(new System.Web.UI.WebControls.ListItem("不限", "0"));
            //this.ddl_Age.SelectedIndex = this.ddl_Age.Items.Count - 1;


        }
        public void BindServiceHouse()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset house = serviceAsset.FirstOrDefault(s => s.Parentid == 1);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == house.Parentid).ToList();
            this.ddl_House.DataTextField = "name";
            this.ddl_House.DataValueField = "id";
            this.ddl_House.DataSource = serviceData;
            this.ddl_House.DataBind();
            //this.ddl_House.Items.Add(new System.Web.UI.WebControls.ListItem("不限", "0"));
            //this.ddl_House.SelectedIndex = this.ddl_House.Items.Count - 1;


        }
        public void BindServiceCar()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset car = serviceAsset.FirstOrDefault(s => s.Parentid == 2);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == car.Parentid).ToList();
            this.ddl_Car.DataTextField = "name";
            this.ddl_Car.DataValueField = "id";

            this.ddl_Car.DataSource = serviceData;
            this.ddl_Car.DataBind();
            //this.ddl_Car.Items.Add(new System.Web.UI.WebControls.ListItem("不限", "0"));
            //this.ddl_Car.SelectedIndex = this.ddl_Car.Items.Count - 1;

        }

        public void BindServiceTrust()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset trust = serviceAsset.FirstOrDefault(s => s.Parentid == 3);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == trust.Parentid).ToList();
            //ServiceAsset blank = new ServiceAsset();
            //blank.Name = "不限制";
            //blank.Id = 0;
            //serviceData.Add(blank);

            this.ddl_Trust.DataTextField = "name";
            this.ddl_Trust.DataValueField = "id";
            this.ddl_Trust.DataSource = serviceData;
            this.ddl_Trust.DataBind();
            //this.ddl_Trust.Items.Add(new System.Web.UI.WebControls.ListItem("不限", "0"));
            //this.ddl_Trust.SelectedIndex = this.ddl_Trust.Items.Count - 1;

        }
        public void BindServiceAgeGroup()
        {
            //guarantee_type.Items.Add(new System.Web.UI.WebControls.ListItem("请选择", "0"));

            //List<AgeGroup> ageGroups = GetAgeGroups();
            //foreach (AgeGroup item in ageGroups)
            //{
            //    guarantee_type.Items.Add(new System.Web.UI.WebControls.ListItem(item.name, item.id.ToString()));
            //}

        }

        public void BindServiceAreaGroup()
        {
            List<ServiceArea> result = null;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceAreaBll())
            {
                result = serviceTypeBll.FindAll().ToList();
            }
            if (result != null)
            {
                List<ServiceArea> serviceData = result.Where(s => s.Level == 2).ToList();
                this.ddlServiceArea.DataTextField = "name";
                this.ddlServiceArea.DataValueField = "id";
                this.ddlServiceArea.DataSource = serviceData;
                this.ddlServiceArea.DataBind();
            }
        }
        public void BindServiceCaiLiao()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset trust = serviceAsset.FirstOrDefault(s => s.Parentid == 189);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == trust.Parentid).ToList();
            this.chkKnowList.DataTextField = "name";
            this.chkKnowList.DataValueField = "id";
            this.chkKnowList.DataSource = serviceData;
            this.chkKnowList.DataBind();

        }

        /// <summary>
        /// 绑定还款方式
        /// </summary>
        public void BindRepayment()
        {
            List<ServiceAsset> serviceAsset = GetAssetData();
            ServiceAsset trust = serviceAsset.FirstOrDefault(s => s.Parentid == 190);

            List<ServiceAsset> serviceData = serviceAsset.Where(s => s.Parentid == trust.Parentid).ToList();
            this.Repayment.DataTextField = "name";
            this.Repayment.DataValueField = "id";
            this.Repayment.DataSource = serviceData;
            this.Repayment.DataBind();
        }
        private List<ServiceAsset> GetAssetData()
        {
            //List<AgeGroup> result = new List<AgeGroup>();
            //return result;

            List<CSP.Model.CSP.ServiceAsset> result;
            using (var serviceTypeBll = CSPBllFactory.GetInstance().GetServiceAssetBll())
            {
                result = serviceTypeBll.FindAll().ToList();
            }
            return result;
        }
        private List<AgeGroup> GetAgeGroups()
        {
            List<AgeGroup> result = new List<AgeGroup>();
            AgeGroup groupOneToThree = new AgeGroup();
            groupOneToThree.id = Convert.ToInt32(AgeGroupType.OneToThree);
            groupOneToThree.name = "20-25岁";

            AgeGroup groupThreeToSix = new AgeGroup();
            groupThreeToSix.id = Convert.ToInt32(AgeGroupType.ThreeToSix);
            groupThreeToSix.name = "25-30岁";

            AgeGroup groupSixToNine = new AgeGroup();
            groupSixToNine.id = Convert.ToInt32(AgeGroupType.SixToNine);
            groupSixToNine.name = "30-35岁";

            AgeGroup groupNineToTwelve = new AgeGroup();
            groupNineToTwelve.id = Convert.ToInt32(AgeGroupType.NineToTwelve);
            groupNineToTwelve.name = "35-40岁";

            AgeGroup groupTwelveToFifteen = new AgeGroup();
            groupTwelveToFifteen.id = Convert.ToInt32(AgeGroupType.TwelveToFifteen);
            groupTwelveToFifteen.name = "40-45岁";

            AgeGroup groupOverFifteen = new AgeGroup();
            groupOverFifteen.id = Convert.ToInt32(AgeGroupType.OverFifteen);
            groupOverFifteen.name = "45-50岁";

            AgeGroup groupOverSixToNine = new AgeGroup();
            groupOverSixToNine.id = Convert.ToInt32(AgeGroupType.OverSixteen);
            groupOverSixToNine.name = "50-55岁";

            result.Add(groupOneToThree);
            result.Add(groupThreeToSix);
            result.Add(groupSixToNine);
            result.Add(groupNineToTwelve);
            result.Add(groupTwelveToFifteen);
            result.Add(groupOverFifteen);
            result.Add(groupOverSixToNine);
            return result;
        }

        protected void btnSumbit_Click(object sender, EventArgs e)
        {
            CSP.Model.CSP.ServiceInfo getServiceInfo = GetServiceInfoDAO();

            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            using (IServiceInfoBll serviceInfoBll = bllFactory.GetServiceInfoBll())
            {
                try
                {
                    getServiceInfo = serviceInfoBll.CreateServiceInfo(getServiceInfo);
                }
                catch (Exception ex)
                {
                    LoggerProvider.Error(ex);
                    this.FailRedirect("提交未失败", "/MyHome/ServiceManage/List.aspx");
                }
            }

            Response.Redirect("~/MyHome/ServiceInfo/PublishOK.aspx?id=" + getServiceInfo.id.ToString());

        }
        public CSP.Model.CSP.ServiceInfo GetServiceInfoDAO()
        {
            CSP.Model.CSP.ServiceInfo _serviceInfo = new Model.CSP.ServiceInfo();
            _serviceInfo.title = this.pdata_name.Text;
            _serviceInfo.username = "cxr";
            //_serviceInfo.productid = this.pdata_type.SelectedItem.Value;
            _serviceInfo.code = pdata_ad.Text.Trim();

            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;

            _serviceInfo.AssetParentId = "52";
            _serviceInfo.ClassValidityStart = new DateTime(year, month, day);
            _serviceInfo.ClassValidityEnd = new DateTime(year, month, day);
            _serviceInfo.CreateTime = new DateTime(year, month, day);
            _serviceInfo.QiXianStart = this.txtQiXianStart.Text.Trim();
            _serviceInfo.QiXianEnd = this.txtQiXianEnd.Text.Trim();
            _serviceInfo.EduStart = this.txtEduStart.Text.Trim();
            _serviceInfo.EduEnd = this.txtEduEnd.Text.Trim();
            _serviceInfo.ShenPiTimeStart = this.txtShenPiTimeStart.Text.Trim();
            _serviceInfo.ShenPiTimeEnd = this.txtShenPiTimeEnd.Text.Trim();
            _serviceInfo.MonthLiXi = this.txtMonthLiXi.Text.Trim();
            _serviceInfo.QianQiCost = this.txtQianQiCost.Text.Trim();
            _serviceInfo.OtherCost = this.txtOtherCost.Text.Trim();
            _serviceInfo.Repayment = this.Repayment.SelectedItem.Value.Trim();
            _serviceInfo.PayDescription = this.PayDescription.Text.Trim();
            _serviceInfo.AssetHouseId = this.RHOUSE.Value.Trim();
            _serviceInfo.AssetCarId = this.RCAR.Value.Trim();
            _serviceInfo.AssetTrustId = this.RTRUST.Value.Trim();
            _serviceInfo.AgeStart = this.RAGE.Value.Trim();
            
            _serviceInfo.AssetParentId = this.radioType.SelectedItem.Value.Trim();
            _serviceInfo.AssetChidId = this.SERVICEINFO.Value.Trim();
            _serviceInfo.Content = this.txtDescriptioin.Text.Trim();
            _serviceInfo.KeyWords = "0";
            _serviceInfo.status = 1;
            _serviceInfo.AssetCaiLiaoId = string.Empty;
            for (int index = 0; index < chkKnowList.Items.Count; index++)
            {
                if (chkKnowList.Items[index].Selected)
                {
                    _serviceInfo.AssetCaiLiaoId += chkKnowList.Items[index].Value.Trim() + ",";
                }
            }

            return _serviceInfo;

        }


    }
}